module.exports=class admin_mod extends require('./model'){
  static getUsersByTypeAndCharMod(type,inputText,CharType,currPage,pageNum){
    pageNum=Number(pageNum);
    currPage=Number(currPage);
    currPage=Number(pageNum*currPage);
    return new Promise((resolve,reject)=>{
      let sql='select * from `user` where '+CharType+' like "%'+inputText+'%" and type='+type+' order by modifytime desc LIMIT ?,?'
      console.log(sql)
      this.query(sql,this.formatParams(currPage,pageNum)).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    })
  }

  static getUserByTypePageTotal(type,inputText,CharType){

    return new Promise((resolve,reject)=>{
      if(inputText.indexOf(',')>=0){
        let classes=inputText.split(',')
        var sql='select count(1) as count from `user` where type='+type+' and '+CharType+' like "%'+classes[0]+'%" or '+CharType+' like "%'+classes[1]+'%"'
      }
      else{
      var sql='select count(1) as count from `user` where type='+type+' and '+CharType+' like "%'+inputText+'%" '
      }
      this.query(sql).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    })
  }

  /**
   * 发布公告
   * @param title
   * @param classes
   */
  static announceMod(content,title,classes){
    return new Promise((resolve,reject)=>{
      let sql="insert into `notice` (title,content,class) values(?,?,?)"
      this.query(sql,this.formatParams(title,content,classes)).then(result=>{
        resolve('发布成功')
      }).catch(err=>{
        reject('发布失败')
      })
    })
  }

  /**获取所有分页通告和数量
   *
   */
  static getAllNoticeMod(pageNum,currPage){
    pageNum=Number(pageNum)
    currPage=Number(currPage)
    currPage=Number(pageNum*currPage)
    return new Promise((resolve, reject) => {
      let sql="select * from notice order by createtime desc LIMIT ?,?"
      this.query(sql,this.formatParams(currPage,pageNum)).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    })
  }

  static getAllNoticeTotal(){
    return new Promise((resolve, reject) => {
      let sql="select count(1) as count from notice"
      this.query(sql).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    })
  }

  /**
   * 删除通告
   */
  static delUserDataMod(n_id){
    return new Promise((resolve,reject)=>{
      let sql="delete from notice where n_id="+n_id
      console.log(sql)
      this.query(sql).then(result=>{
        resolve('删除通告成功')
      }).catch(err=>{
        reject('删除通告失败')
      })
    })
  }

  /**
   * 根据id获取notice的详细内容
   * @param n_id
   * @returns {Promise<unknown>}
   */
  static getnoticeByidMod(n_id){
    return new Promise((resolve, reject) => {
      let sql="select * from notice where n_id="+n_id
      this.query(sql).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    })
  }

  static UpdataNoticeMod(n_id,content){
    return new  Promise((resolve, reject) => {
      let sql="update notice set content='"+content+"' where n_id="+n_id
      this.query(sql).then(result=>{
        resolve('保存成功')
      }).catch(err=>{
        reject('保存失败')
      })
    })
  }

  /**
   *
   * @returns {Promise<unknown>}
   */
  static getclassMod(){
    return new Promise((resolve, reject) => {
      let sql="select * from class"
      this.query(sql).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    })
  }


  /**
   * 教师获取自己班级的请假申请
   * @param req
   * @param res
   * @returns {Promise<void>}
   */
  static getleaveMod(classArr,currPage,pageNum){
    pageNum=Number(pageNum)
    currPage=Number(currPage)
    currPage=Number(pageNum*currPage)
    return new Promise((resolve, reject) => {
      let sql="select * from `leave` where state=0 and classes= "
      for (let i=0;i<classArr.length;i++){
        if(i==0) sql+="'"+classArr[i] +"'"
        else sql+=" or classes= '"+ classArr[i]+"'"
      }
      sql+=" order by createtime desc limit "+currPage+","+pageNum
      console.log(sql)
      this.query(sql).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    })
  }
  static getleaveTotal(classArr){
    return new Promise((resolve, reject) => {
      let sql="select count(1) as count from `leave` where classes="
      for (let i=0;i<classArr.length;i++){
        if(i==0) sql+="'"+classArr[i] +"'"
        else sql+=" or classes= '"+ classArr[i]+"'"
      }
      console.log(sql)
      this.query(sql).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    })
  }

  /**
   * 修改请假管理的状态
   * @param req
   * @param res
   * @returns {Promise<void>}
   */
  static updataLeaveStateMod(l_id,state){
    let sql="update `leave` set state= ? where l_id=?"
    return new Promise((resolve, reject) => {
      let sql="update `leave` set state= ? where l_id=?"
      this.query(sql,this.formatParams(state,l_id)).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    })
  }

  /**
   * 添加班级
   * @param classes
   * @param college
   */
  static addclassesMod(classes,college){
    return new Promise((resolve, reject) => {
      let sql="insert into `class` (classes,college) values(?,?)"
      this.query(sql,this.formatParams(classes,college)).then(result=>{
        resolve('添加成功')
      }).catch(err=>{
        reject('添加失败')
      })
    })
  }



 static noticereadMod(n_id){
    return new Promise((resolve, reject) => {
      let sql="select count(1) as count from `read` where n_id= "+n_id
      this.query(sql).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    })
 }
}


